module.exports = (app) => {
  const { STRING, TEXT, INTEGER, DATE, BOOLEAN } = app.Sequelize
  const Article = app.model.define('article', {
    id: {
      type: INTEGER(20).UNSIGNED,
      primaryKey: true,
      autoIncrement: true,
      comment: '主键id'
    },
    detail_id: {
      type: INTEGER(20).UNSIGNED,
      allowNull: false,
      comment: '文章id',
      references: {
        model: 'article_detail',
        key: 'id'
      },
      onDelete: 'CASCADE'
    },
    title: {
      type: STRING(100),
      allowNull: false,
      comment: '标题'
    },
    desc: {
      type: TEXT,
      allowNull: false,
      comment: '描述'
    },
    poster: {
      type: STRING(200),
      allowNull: false,
      comment: '封面图'
    },
    likes: {
      type: INTEGER(11),
      allowNull: false,
      defaultValue: 0,
      comment: '点赞人数'
    },
    views: {
      type: INTEGER(11),
      allowNull: false,
      defaultValue: 0,
      comment: '浏览人数'
    },
    status: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: 1,
      comment: '状态 0:禁用 1:启用'
    },
    created_at: {
      type: DATE,
      get() {
        return new Date(this.getDataValue('created_at')).getTime()
      }
    },
    updated_at: DATE
  })
  Article.associate = function () {
    // 一对一关系
    Article.hasOne(app.model.ArticleDetail, {
      foreignKey: 'id', // 外键在 Article 表中
      // sourceKey: 'detail_id' // 关联字段在 Article 表中
    })
  }
  return Article
}
